
************************************************
*Start from raw micro-level admin data from IGSS 
*in the IGSS server 
***********************************************

forval y=2002/2020{
import sas  using "F:\IN\j_characteristics_`y'.sas7bdat", clear 
save "F:\WORK\j_characteristics_`y'.dta", replace
}


use "F:\WORK\j_characteristics_2002.dta", clear
forval y=2003/2020{
append using "F:\WORK\j_characteristics_`y'.dta"
}
save "F:\WORK\j_characteristics_panel.dta", replace


forval y=2002/2020{
import sas  using "F:\IN\i_sdc_`y'.sas7bdat", clear 
save "F:\WORK\i_sdc_`y'.dta", replace   /* individual dataset at monthly level */
}
forval y=2002/2020{
  use "F:\WORK\i_sdc_`y'.dta", clear     /* one individual -> several jobs */
  merge 1:m individual_IDnumber reference_period using "F:\WORK\j_characteristics_`y'.dta"
  g date=date( reference_period, "YM" )
  g year=year(date)
  g hourly_wage_b=j_month_total_wage/j_nb_total_worked_hours
  g hourly_wage=j_hourly_total_wage
  replace hourly_wage=hourly_wage_b if missing(j_hourly_total_wage)
  destring e_activity_sector, replace
  drop if j_posted_worker==-8 /*drop self employed */
  drop if j_job_status>0  /*one individual -> several jobs */
  collapse (mean) hourly_wage j_nb_total_worked_hours e_activity_sector  i_first_job_Luxembourg_date j_blue_collar (sum) j_posted_worker, by(individual_IDnumber employer_IDnumber)
  save "F:\WORK\indiv_`y'.dta", replace
   }
   
   forval y=2002/2020{
   use "F:\WORK\indiv_`y'.dta", clear
   g year=`y'
   save  "F:\WORK\indiv_`y'_y.dta", replace
   }
   
    forval y=2002/2020{
    use "F:\WORK\j_characteristics_`y'.dta", clear 
    g year=substr(reference_period,1,4 )
    g date=date( reference_period, "YM" )
    format date %td
    g freq=1
	g hourly_wage_b=j_month_total_wage/j_nb_total_worked_hours
    g hourly_wage=j_hourly_total_wage
    replace hourly_wage=hourly_wage_b if missing(j_hourly_total_wage)
    destring e_activity_sector, replace
    drop if j_posted_worker==-8 /*drop self employed*/
    drop if j_job_status>0  /*one individual -> several jobs even within employer and month*/
	destring year, replace
	*only account for one employee per month -> no double count of people with several contracts per month 
	collapse (sum) j_posted_worker j_nb_total_worked_hours j_month_total_wage (mean) hourly_wage  e_activity_sector year j_blue_collar, by(date employer_IDnumber individual_IDnumber)
	g ind_posting=0
	replace ind_posting=1 if j_posted_worker>0 
	g freq=1
	collapse (sum) freq j_posted_worker j_nb_total_worked_hours j_blue_collar ind_posting j_month_total_wage (mean) hourly_wage  e_activity_sector year, by(date employer_IDnumber)
	collapse (mean)  freq j_posted_worker  j_blue_collar hourly_wage  e_activity_sector ind_posting year (sum) j_nb_total_worked_hours j_month_total_wage, by( employer_IDnumber)
	   save  "F:\WORK\ent_`y'.dta", replace

   }
   
   use "F:\WORK\ent_2002.dta", clear 
   forval y=2003/2020{
   append using  "F:\WORK\ent_`y'.dta"
   }
   drop if employer_IDnumber=="-8"
   keep if e_activity_sector==int(e_activity_sector)
   preserve 
   collapse (sum) j_posted_worker ind_posting freq, by(year e_activity_sector )
      g frac=ind_posting/freq

   g label="."
   replace label="Chemical and petroleum treatment" if e_activity_sector==19
   replace label="Freight Transport" if e_activity_sector==49
   replace label="Other Chemical and petroleum treatment services" if e_activity_sector==9
   replace label="Food processing" if e_activity_sector==10
   replace label="Other Chemical and petroleum treatment services" if e_activity_sector==9
   replace label="Other Transport" if e_activity_sector==50
   replace label="Construction of Residential Building" if e_activity_sector==41
   replace label="Construction of civil engineering projects" if e_activity_sector==42
   replace label="Specialised construction activities" if e_activity_sector==43
   replace label="Specialised construction activities" if e_activity_sector==44
   replace label="Temporary Employment Agencies" if e_activity_sector==78
   replace label="Building Services" if e_activity_sector==81
   replace label="Show-business" if e_activity_sector==90
   replace label="Waste treatment services" if e_activity_sector==38
   replace label="Manufacture of metal products" if e_activity_sector==25
   replace label="Installation and repair of machinery" if e_activity_sector==33
   replace label="Wholesale" if e_activity_sector==46
   replace label="Technical Services Provision" if e_activity_sector==71
   replace label="Finance" if e_activity_sector==64
   replace label="Consulting" if e_activity_sector==70
   replace label="Law and Accounting" if e_activity_sector==69
   replace label="Other specialised Transport Services" if e_activity_sector==52
   replace label="Software programming and other ICT" if e_activity_sector==62
   
   twoway (connected ind_posting year if e_activity_sector==49) ///
   (connected ind_posting year if e_activity_sector==43) ///
   (connected ind_posting year if e_activity_sector==78) ///
   (connected ind_posting year if e_activity_sector==46) 
   
   egen tot=sum(freq), by(year)
   egen post=sum(ind_posting), by(year)
   g frac_p=ind_posting/post
   graph hbar frac_p frac if label!="." & year>2004, over(label, sort(frac_p) lab(labsize(small)) ) graphregion(fcolor(white) lcolor(white)) ///
   legend(label(1 "Posted Workers/Total Posted Workers") label(2 "Posted Workers/Total Sectoral Employment") size(*0.7) cols(1)) ///
   ytitle("Share of Posted Workers")
       graph export "F:\WORK\Figures\DS\histo_postedworkers.pdf", replace
	   
    g mob=0
   foreach y in 33 49 41 43 78 42 81{
   replace mob=1 if e_activity_sector==`y'
   
    }

	tab mob
	
   restore 
   
   g indic_posti=0
   replace indic_posti=1 if ind_posting>0
   bysort employer_IDnumber (year) : gen cum=sum(indic_posti)
   
   g event=year if indic_posti==1 & cum==1
   
   egen event_d=min(event), by(employer_IDnumber)
   replace event_d=0 if missing(event_d)
   g time=year-event_d
   replace time=-1 if time==year
   g treated=0
   replace treated=1 if event_d>0
   
   sum time
   
   g timed=time+18
   
   
   forval y=0/36{
   g coeff_`y'=0
   replace coeff_`y'=1 if timed==`y'
   }
   
   g zero=1
   label var zero "-1"
   
   forval y=0/36{
   local j=`y'-18
   label var coeff_`y' "`j'"
   }
   
   g coeff_bottom=0
   replace coeff_bottom=1 if time<-4
   g coeff_up=0
   replace coeff_up=1 if time>4
     

   g logwage=log(hourly_wage)
   g logemp=log(freq)
   g loghours=log(j_nb_total_worked_hours)
   g logwagebill=log(j_month_total_wage)
   
   
   egen id=group(employer_IDnumber)
   xtset id year 
   keep if e_activity_sector==int(e_activity_sector)
   drop if e_activity_sector<0
   
   *list of sector where services can only be exported through posting (to map with Portuguese definition of posting supply)
   
   g mob=0
   foreach y in 33 49 41 43 78 42 81{
   replace mob=1 if e_activity_sector==`y'
   }
	
	
   bys mob: tab treated if year==2015
   bys mob: tab indic_posti
   bys mob: tab indic_posti if year==2015
   tab indic_posti if year==2015
   tab treated if year==2015

   count if time==0 & treated==1
   g frac=ind_posting/freq
   g frac_bluecollar=ind_posting/j_blue_collar
   sum frac if indic_posti==1, d
   sum frac if indic_posti==1 & time==0, d
   tab treated  if year==2015
   tab indic_posti  if year==2015
   preserve 
   collapse (sum) treated (mean) freq, by(employer_IDnumber)
   g indic=0 
   replace indic=1 if treated>0
   g tagix=1
   collapse (sum) tagix, by(indic)
   
   restore
   
   preserve 
   g tagix=1
   collapse (sum) freq tagix, by(indic_posti year)
   restore 
   *Descriptive stat: posting firms vs others 
   
   sum frac if indic_posti==1 & time==0, d
   sum frac if indic_posti==1 & time==0 & mob==0, d
   sum frac if indic_posti==1 & time==0  & mob==1, d

   su frac if time>-1 & mob==0 & indic_posti==1
   su frac if time>-1 & mob==1 & indic_posti==1
   su frac if time>-1 & indic_posti==1
   
   g tag=1
   preserve
   collapse (sum) freq tag indic_posti treated, by(mob year)
   g s_ever=treated/tag 
   g s_exp=indic_posti/tag 

   restore
   
   
   
   
   preserve
   collapse (sum) freq tag indic_posti ind_posting, by(mob treated year)
   restore

   egen panel_export=sum(indic_posti), by(employer_IDnumber)
   
   egen median=median(freq), by(employer_IDnumber)
   drop if median<3
   
   *only keep firms where we observe at least 2 years without treatment
   drop if event_d<2007 & treated==1
   drop if event_d>2017 & treated==1
   
   egen clusterb=group(event_d e_activity_sector)
   
   preserve 
   keep if treated==1
   collapse (mean) frac frac_bluecollar, by(time mob)
   
   twoway (connected frac time if mob==1 ), ///
   xlabel(-15 (1) 13) graphregion(fcolor(white) lcolor(white)) ///
   ytitle("Posted Workers/Total Payroll") ylabel(0.05 "5%" 0.1 "10%" 0.15 "15%") ///
   xtitle("Time to First Posting")
   graph export "F:\WORK\Figures\DS\treatmentintensity.pdf", replace

   
   twoway (connected frac time if mob==0) (connected frac_bluecollar time if mob==0), ///
   xlabel(-15 (1) 13)
   
   restore 
   
  ********************************************************************
  * Figure 11 (for Luxembourg lines)
  *******************************************************************

   reghdfe logemp coeff_bottom coeff_14-coeff_16 zero coeff_18-coeff_22 coeff_up if treated==1 & mob==1, absorb(year##e_activity_sector employer_IDnumber ) cl(clusterb)
   
  coefplot, keep(coeff_14 coeff_15 coeff_16 zero coeff_18 coeff_19 coeff_20 coeff_21 coeff_22) omit vertical graphregion(fcolor(white) lcolor(white)) ciopts(recast(rcap) lwidth(medthick)) ///
  yline(0, lpattern(dash) lcolor(gs10)) ylabel(-.2 (.2) .6) xline(4.5, lcolor(red)) scale(1.2)
  graph export "F:\WORK\Figures\DS\ES_ent_logemp.pdf", replace
  
   reghdfe loghours coeff_bottom coeff_14-coeff_16 zero coeff_18-coeff_22 coeff_up if treated==1 & mob==1, absorb(year##e_activity_sector employer_IDnumber ) cl(clusterb)
   
  coefplot, keep(coeff_14 coeff_15 coeff_16 zero coeff_18 coeff_19 coeff_20 coeff_21 coeff_22) omit vertical graphregion(fcolor(white) lcolor(white)) ciopts(recast(rcap) lwidth(medthick)) ///
  yline(0, lpattern(dash) lcolor(gs10)) ylabel(-.2 (.2) .6) xline(4.5, lcolor(red)) scale(1.2)
  graph export "F:\WORK\Figures\DS\ES_ent_loghours.pdf", replace
 

   
      reghdfe logwage coeff_bottom coeff_14-coeff_16 zero coeff_18-coeff_22 coeff_up if treated==1 & mob==1, absorb(year##e_activity_sector employer_IDnumber ) cl(clusterb)
   est store wage_ent
  coefplot, keep(coeff_14 coeff_15 coeff_16 zero coeff_18 coeff_19 coeff_20 coeff_21 coeff_22) omit vertical graphregion(fcolor(white) lcolor(white)) ciopts(recast(rcap) lwidth(medthick)) ///
  yline(0, lpattern(dash) lcolor(gs10)) ylabel(-.2 (.2) .6) xline(4.5, lcolor(red)) scale(1.2)
  graph export "F:\WORK\Figures\DS\ES_ent_logwage.pdf", replace
  
   reghdfe logwagebill coeff_bottom coeff_14-coeff_16 zero coeff_18-coeff_22 coeff_up if treated==1 & mob==1, absorb(year##e_activity_sector employer_IDnumber ) cl(clusterb)
   est store wage_ent
  coefplot, keep(coeff_14 coeff_15 coeff_16 zero coeff_18 coeff_19 coeff_20 coeff_21 coeff_22) omit vertical graphregion(fcolor(white) lcolor(white)) ciopts(recast(rcap) lwidth(medthick)) ///
  yline(0, lpattern(dash) lcolor(gs10)) ylabel(-.2 (.2) .6) xline(4.5, lcolor(red)) scale(1.2)
    graph export "F:\WORK\Figures\DS\ES_ent_logwagebill.pdf", replace

  
  